【チュートリアル】Ansible を使用した Ubuntu へのコントロールプレーン LB のデプロイ
これは開発ドキュメントであり、開発者および AI を対象としています。
本稿では Ubuntu を例に、Ansible を使用してシングルノードの HAProxy をデプロイし、RKE2/Kubernetes コントロールプレーンエントリの L4 TCP 負荷のみを処理します。
適用範囲
- コントロールプレーンエントリ(
6443/9345)のみを処理し、業務トラフィックや Ingress は担当しません。 - シングルノード LB であり、高可用性は備えていません。
- パブリッククラウドにマネージド L4 LB がない場合、またはコスト制限がある場合に利用可能です。
前提条件
- LB ノードとして 1 台の Ubuntu サーバー。
- LB とコントロールプレーンノードは同一リージョン/同一ネットワーク内にあり、遅延の安定性を優先します。
- コントロールプレーンノードに対して
6443/TCPと9345/TCPを開放します。
Ansible プロジェクトの初期化
リポジトリの初期化
まずフォルダを作成します。プロジェクト名を lb-ansible と仮定します。
yun@yun ~/V/a/y/p/ansible (main)> mkdir lb-ansible
yun@yun ~/V/a/y/p/ansible (main)> ls
lb-ansible/
プロジェクトリポジトリに入り、git を初期化し、GitHub リポジトリを作成します。
cd lb-ansible
git init
echo "# lb-ansible" > README.md
git add .
git commit -m "chore: initial commit"
gh repo create lb-ansible --private --source=. --remote=origin --push
以下のコードは任意であり、新しく作成したコードリポジトリをサブモジュールとして宣言するために使用します。
cd ..
rm -rf lb-ansible/
git submodule add https://github.com/<用户名或组织>/lb-ansible.git ./lb-ansible
ディレクトリ構造の計画
次にプロジェクト構造を分割します。
mkdir -p inventories/prod \
group_vars \
playbooks \
templates
micro を使用してファイルを作成および編集します。
micro ansible.cfg \
inventories/prod/hosts.yml \
group_vars/lb.yml \
playbooks/lb.yml \
templates/haproxy.cfg.j2
ディレクトリ構造は以下の通りです。
lb-ansible/
├── ansible.cfg
├── inventories/
│ └── prod/
│ └── hosts.yml
├── group_vars/
│ └── lb.yml
├── playbooks/
│ └── lb.yml
└── templates/
└── haproxy.cfg.j2
配置 Ansible
micro ansible.cfg :
[defaults]
inventory = inventories/prod/hosts.yml
remote_user = root
host_key_checking = False
timeout = 30
编写 inventory
micro inventories/prod/hosts.yml :
all:
children:
lb:
hosts:
lb-1:
SSH Config 内のエイリアスを直接使用できるため、
ansible_hostを記述する必要はありません。
编写变量
micro group_vars/lb.yml :
haproxy_bind_ip: "0.0.0.0"
haproxy_api_port: 6443
haproxy_reg_port: 9345
rke2_api_backends:
- name: rke2-server1
host: 10.0.0.11
- name: rke2-server2
host: 10.0.0.12
- name: rke2-server3
host: 10.0.0.13
编写模板
micro templates/haproxy.cfg.j2 :
global
log /dev/log local0
maxconn 4096
defaults
mode tcp
timeout connect 5s
timeout client 30s
timeout server 30s
frontend rke2_api
bind {{ haproxy_bind_ip }}:{{ haproxy_api_port }}
default_backend rke2_api
backend rke2_api
option tcp-check
default-server inter 2s fall 3 rise 2
{% for backend in rke2_api_backends %}
server {{ backend.name }} {{ backend.host }}:{{ haproxy_api_port }} check
{% endfor %}
frontend rke2_reg
bind {{ haproxy_bind_ip }}:{{ haproxy_reg_port }}
default_backend rke2_reg
backend rke2_reg
option tcp-check
default-server inter 2s fall 3 rise 2
{% for backend in rke2_api_backends %}
server {{ backend.name }} {{ backend.host }}:{{ haproxy_reg_port }} check
{% endfor %}
编写 Playbook
micro playbooks/lb.yml :
- name: Deploy control plane LB
hosts: lb
become: true
tasks:
- name: Install haproxy
ansible.builtin.apt:
name: haproxy
state: present
update_cache: true
- name: Deploy haproxy config
ansible.builtin.template:
src: templates/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
owner: root
group: root
mode: "0644"
notify: Restart haproxy
- name: Enable haproxy
ansible.builtin.service:
name: haproxy
enabled: true
state: started
handlers:
- name: Restart haproxy
ansible.builtin.service:
name: haproxy
state: restarted
执行与验证
デプロイを実行します。
ansible-playbook playbooks/lb.yml
ポートを確認します。
ss -lntp | grep -E ":6443|:9345"
コントロールプレーンエントリには LB ノードの IP またはドメイン名を使用し、RKE2 変数で
rke2_api_ipをそのアドレスに設定します。